#!/bin/bash

echo "═══════════════════════════════════════════════"
echo "     Backend Update Deployment Script          "
echo "═══════════════════════════════════════════════"
echo ""
echo "This script will help you deploy backend updates to the production server"
echo ""

# Server details
SERVER_IP="34.31.89.197"
SERVER_USER="zjsnxc"
BACKEND_PATH="/home/zjsnxc/kedge-backend"

echo "📋 Steps to deploy backend updates:"
echo ""
echo "1. First, upload this script and the backend folder to the server:"
echo "   scp -r backend/ ${SERVER_USER}@${SERVER_IP}:${BACKEND_PATH}/"
echo ""
echo "2. SSH into the server:"
echo "   ssh ${SERVER_USER}@${SERVER_IP}"
echo ""
echo "3. Run the database migration for class field:"
echo "   cd ${BACKEND_PATH}"
echo "   # Apply the migration to add class field"
echo "   sudo -u postgres psql -d kedge_db << 'EOF'"
echo "   -- Add class field to users table"
echo "   ALTER TABLE kedge_practice.users "
echo "   ADD COLUMN IF NOT EXISTS class VARCHAR(50);"
echo "   "
echo "   -- Create index for efficient class-based queries"
echo "   CREATE INDEX IF NOT EXISTS idx_users_class ON kedge_practice.users(class);"
echo "   "
echo "   -- Verify the column was added"
echo "   \\d kedge_practice.users"
echo "   EOF"
echo ""
echo "4. Build and restart the backend service:"
echo "   cd ${BACKEND_PATH}/backend"
echo "   pnpm install"
echo "   nx build api-server"
echo "   # Restart the backend service (adjust based on your setup)"
echo "   pm2 restart api-server || pm2 start dist/packages/apps/api-server/main.js --name api-server"
echo ""
echo "5. Verify the deployment:"
echo "   curl http://localhost:8718/v1/health"
echo "   curl http://${SERVER_IP}:8718/v1/health"
echo ""
echo "6. Test the new features:"
echo "   # Test quiz validation fix"
echo "   # Test class field in user registration"
echo ""

echo "═══════════════════════════════════════════════"
echo "📝 Key Changes in This Deployment:"
echo "═══════════════════════════════════════════════"
echo ""
echo "1. Quiz Validation Fix:"
echo "   - Fixed single-choice quiz validation"
echo "   - Fixed multiple-choice quiz validation"
echo "   - Answers are now correctly compared"
echo ""
echo "2. Class Field Feature:"
echo "   - Added class field to users table"
echo "   - Students must have a class assigned"
echo "   - Teachers and admins can have null class"
echo "   - Bulk registration templates updated"
echo ""
echo "3. Files Changed:"
echo "   - backend/packages/libs/practice/src/lib/practice.service.ts"
echo "   - backend/packages/libs/models/src/auth/auth.repository.schema.ts"
echo "   - backend/packages/apps/api-server/src/app/controllers/admin.controller.ts"
echo "   - backend/packages/apps/api-server/src/app/controllers/auth.controller.ts"
echo "   - backend/templates/* (new bulk registration templates)"
echo ""

echo "═══════════════════════════════════════════════"
echo "⚠️  Important Notes:"
echo "═══════════════════════════════════════════════"
echo ""
echo "1. Make sure to backup the database before running migrations"
echo "2. Test the endpoints after deployment"
echo "3. Monitor logs for any errors: pm2 logs api-server"
echo "4. The frontend deployment package is in deployment.tar.gz"
echo ""